package org.brisling.satisfaction.service;

import java.util.Date;
import java.util.List;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;
import org.brisling.common.util.LongUtil;
import org.brisling.satisfaction.domain.SatisEvaluate;
import org.brisling.satisfaction.repository.SatisEvaluateRepo;
import org.brisling.sysman.domain.Users;
import org.brisling.sysman.queryService.QueryUserService;
import org.brisling.sysman.service.base.BaseSysmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/satisEvaluate")
/**
 */
public class SatisEvaluateService extends BaseSysmanService<SatisEvaluate>{
	
	@SuppressWarnings("unused")
	@Autowired
	private SatisEvaluateRepo baseRepo;
	
	public SatisEvaluateService(){
		this.moduleName = ConstSysParam.MODULE_SATISFACTION_EVALUATE;
		this.clazz = SatisEvaluate.class;
	}
	
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return getBizMainPage();
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return getBizEditPage();
	}
	
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getListDomain.action")
	public ListDomain<SatisEvaluate> getListDomain(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
				
		return super.getPagedList(page, rows);
			
	}
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getList.action")
	public List<SatisEvaluate> getPagedListRows(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
		
			return super.getAll(page,rows,getSort(sort));
			
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/del.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
				
		return super.delRows(id, ids);		 
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delFlowRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
		
			return delRow(id,ids);
		
	}
	
	
	/**
	 * 保存窗口满意度评价结果数据
	 * @param id
	 */
	@RequestMapping("/save.action")
	public SatisEvaluate save(@RequestBody String body){
		SatisEvaluate obj = bodyToObj(body, SatisEvaluate.class);
		super.save(obj);
		return obj;
	}
	
	
	@Autowired
	private QueryUserService userService;
	/**
	 * 保存远端(客户端)窗口满意度评价结果数据
	 * @param id
	 */
	@RequestMapping("/remoteSave.action")
	public RetDomain remoteSave(
			@RequestParam(value="userid",required=false) String userid,	
			@RequestParam(value="username",required=false) String username,
			@RequestParam(value="patientno",required=false) String patientno,
			@RequestParam(value="patientname",required=false) String patientname,
			@RequestParam(value="outpatient_number",required=false) String outpatient_number,
			@RequestParam(value="type",required=false) Integer type,
			@RequestParam(value="result",required=false) Integer result,
			@RequestBody String body){
		RetDomain domain = null;
		
		System.out.println("userid: " + userid);
		System.out.println("username: " + username);
		System.out.println("patientno: " + patientno);
		System.out.println("patientname: " + patientname);
		System.out.println("outpatient_number: " + outpatient_number);
		System.out.println("type: " + type);
		System.out.println("result: " + result);
		
		if(userid!=null && result!=null){
			
			System.out.println("....进入获取用户信息..... " );
			//获取用户信息
			Users u = SysConfig.getUsr(userid);	
			if(u==null){
				u = userService.getOne(LongUtil.getLongValue(userid, null));
			}
			
			//获取用户信息
			if(u!=null && u.getId()!=null){
				System.out.println("....查找用户信息成功..... " );
				SatisEvaluate se = new SatisEvaluate();
				se.setDoct_id(userid);
				se.setDoct_name(u.getName());
				se.setDepart_name(u.getDeptName());
				se.setDeptid(u.getDeptid().toString());
				se.setResult(result.toString());
				se.setEvaluateat(new Date());
				
				//判断患者信息
				if(patientno!=null){
					System.out.println("....获取患者信息成功..... " );
					se.setPatient_no(patientno);
					se.setPatient_name(patientname);
				}
				
				//判断门诊号信息是否为空
				if(outpatient_number!=null){
					System.out.println("....获取门诊号码信息成功..... " );
					se.setOutpatient_number(outpatient_number);
				}
				
				//判断业务类型
				if(type!=null){
					System.out.println("....获取业务类型信息成功..... " );
					se.setYwlxcode(type);
				}
				
				
				super.save(se);
				System.out.println("保存成功");
				domain = RetDomain.successDomain("保存成功");
			}else{
				System.out.println("获取用户信息失败");
				domain = RetDomain.errorDomain("获取用户信息失败");
			}
			
			
		}else{
			System.out.println("无效评价参数");
			domain = RetDomain.errorDomain("无效评价参数");
		}
		return domain;
	}
	
	/**
	 * 保存窗口满意度评价信息	
	 */
	public SatisEvaluate save(SatisEvaluate info){
		
		super.save(info);
		return info;
	}
	
	/**
	 * 批量保存
	 * @param lists
	 * @return
	 */
	public RetDomain save(List<SatisEvaluate> lists){
		RetDomain domain = new RetDomain();
		if(lists!=null && lists.size()>0){
			return super.saveList(lists);
		}else{
			domain.setErrorMsg("保存数据不能为空");
			return domain;
		}
	}	
}
